<template>
  <section class="todoapp">
    <TodoHeader @zen="kenFn"></TodoHeader>
    <TodoMain :arr="lists" @san="sanFn"></TodoMain>
    <TodoFooter
      :sheng="shengFn"
      :list="list"
      @activeFn="activeDn"
      @clsFn="clearFn"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
export default {
  data() {
    return {
      type: "all",
      list: JSON.parse(localStorage.getItem("todos")) || [
        {
          id: 100,
          name: "吃饭",
          isDone: true,
        },
        {
          id: 201,
          name: "睡觉",
          isDone: false,
        },
        {
          id: 103,
          name: "打豆豆",
          isDone: true,
        },
      ],
    };
  },
  methods: {
    //删除父传子
    sanFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    //添加父传子
    kenFn(name) {
      // console.log(name);
      this.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      });
    },
    //清除完成的
    clearFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },

    activeDn(all) {
      this.type = all;
    },
  },
  components: {
    TodoFooter,
    TodoHeader,
    TodoMain,
  },
  computed: {
    //剩余数量父传子
    shengFn() {
      return this.list.filter((item) => !item.isDone).length;
    },
    lists() {
      if (this.type === "all") {
        return this.list;
      } else if (this.type === "active") {
        return this.list.filter((item) => item.isDone === false);
      } else if (this.type === "over") {
        return this.list.filter((item) => item.isDone === true);
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todos", JSON.stringify(this.list));
      },
    },
  },
};
</script>
